﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Data.AID
{
    public class Consideracao
    {
        public int Id { get; set; }
        public int IdGoal { get; set; }
        public string Consideration { get; set; }
        public string Approver { get; set; }
    }

    public class ConsideracaoDAO
    {
        SPSite Site = SPContext.Current.Site;
        SPWeb Web = SPContext.Current.Web;

        public void CreateConsideration(Consideracao consideration)
        {
            using (SPSite eSite = new SPSite(Site.ID))
            {
                using (SPWeb eWeb = eSite.OpenWeb(Web.ID))
                {
                    eWeb.AllowUnsafeUpdates = true;

                    SPListItemCollection Lista = eWeb.Lists["ConsideracaoMeta"].Items;
                    SPListItem item = Lista.Add();

                    item["IdMeta"] = consideration.IdGoal;
                    item["Consideracao"] = consideration.Consideration;
                    item["Aprovador"] = consideration.Approver;

                    item.Update();

                    eWeb.AllowUnsafeUpdates = false;
                }
            }
        }
        public void DeleteConsiderationByGoal(List<Consideracao> listConsideration)
        {
            using (SPSite eSite = new SPSite(Site.ID))
            {
                using (SPWeb eWeb = eSite.OpenWeb(Web.ID))
                {
                    eWeb.AllowUnsafeUpdates = true;

                    SPListItemCollection Lista = eWeb.Lists["ConsideracaoMeta"].Items;

                    foreach (Consideracao consideration in listConsideration)
                    {
                        SPListItem item = Lista.GetItemById(consideration.Id);
                        item.Delete();
                    }

                    eWeb.AllowUnsafeUpdates = false;
                }
            }
        }
        public List<Consideracao> GetAllConsiderations()
        {
            using (SPSite eSite = new SPSite(Site.ID))
            {
                using (SPWeb eWeb = eSite.OpenWeb(Web.ID))
                {
                    return eWeb.Lists["ConsideracaoMeta"].Items.Cast<SPListItem>()
                        .Select(p => new Consideracao
                        {
                            Id = Convert.ToInt32(p["ID"]),
                            IdGoal = new SPFieldLookupValue(p["IdMeta"].ToString()).LookupId,
                            Consideration = p["Consideracao"] != null ? p["Consideracao"].ToString() : "",
                            Approver = p["Aprovador"] != null ? p["Aprovador"].ToString() : ""
                        }).ToList();
                }
            }
        }
        public List<Consideracao> GetAllConsiderationsByGoal(int idGoal)
        {
            return GetAllConsiderations().Where(p => p.IdGoal == idGoal).ToList();
        }
    }
}
